const fs = require('fs');
const config = require('../config/config.js')

const log = (params) => {
    params['date'] = new Date();
    mkCdir(params);
};

/**
 * 创建当前文件
 * 
*/
function mkCdir(params){
    const currentDate = new Date();
    const year = currentDate.getFullYear();
    const month = currentDate.getMonth();
    const date = currentDate.getDate();
    mkd('logs')(year)(month)(date)();
    const urlPath =  config.BASE_PATH + '/../logs/'+ currentDate.getFullYear() + '/' + currentDate.getMonth() + '/' + currentDate.getDate()
    fs.appendFile(urlPath + '/log.log', JSON.stringify(params), (err)=>{
        if(err){
            console.error(err);           
        };
        console.log('成功写入');
    })

}
/**
 * 创建
 * @param {*} path 
 */
function mkd (path) {
    const func = (str)=>{
        const urlPath = config.BASE_PATH + '/../' + path;
        const dirs = fs.existsSync(urlPath);
        if(!dirs){
                // 不存在
                fs.mkdirSync(urlPath)
        }
        path += '/' + str;
        return func;
    }
    func.toString = func.valueOf = () => path;
    return func;
}
module.exports = log;